package com.gzyj.web.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gzyj.data.common.ResponseMsg;
import com.gzyj.data.web.user.UserLoginParam;
import com.gzyj.pojo.web.user.BladeUser;
import com.gzyj.web.mapper.user.BladeUserMapper;
import com.gzyj.web.utils.DigestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/user")
public class UserApi {

    @Autowired
    private BladeUserMapper bladeUserMapper;

    @PostMapping("doLogin")
    public ResponseMsg<BladeUser> doLogin(UserLoginParam param) {

        BladeUser user = bladeUserMapper.selectOne(new QueryWrapper<BladeUser>().eq("account", param.getUsername()).eq("password", DigestUtil.encrypt(param.getPassword())));

        if (user != null) {
            user.setPassword(null);
            if (user.getAccount().equalsIgnoreCase("admin") || user.getAccount().equalsIgnoreCase("yj-admin")) {
                user.setIsAdmin(true);
            }else{
                user.setIsAdmin(false);
            }
            return new ResponseMsg<>(200, "success", user);
        } else {
            return new ResponseMsg<>(201, "用户名或密码错误");
        }

    }

}
